package com.mz.ozy.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.tools.ScreenUtils;
import com.mz.ozy.R;
import com.mz.ozy.activity.c2c.PhotoScanActivity;
import com.mz.ozy.activity.c2c.SellKtActivity;
import com.mz.ozy.activity.shop.CommentActivity;
import com.mz.ozy.bean.CourseCommentBean;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.CommentDecoration;
import com.mz.ozy.widget.ExpandCommentView;
import com.mz.ozy.widget.ExpandTextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2019/3/22.
 */

public class CourseCommentAdapter extends BaseQuickAdapter<CourseCommentBean, BaseViewHolder> {

    private RecyclerView mGvCommentImgs;
    private Activity mActivity;

    public CourseCommentAdapter(Activity activity, List<CourseCommentBean> data) {
        super(R.layout.course_course_comment_item, data);
        mActivity = activity;
    }

    @Override
    protected void convert(BaseViewHolder helper, CourseCommentBean item) {
        helper.setText(R.id.tv_detail_comment_name, item.getNickname());
        helper.setText(R.id.tv_detail_comment_time, item.getCreated() + " | " + item.getArrribute_name());
        helper.setText(R.id.tv_detail_comment_content, Utils.getDecodeStr(item.getContent()));

        if (TextUtils.isEmpty(item.getUseravatar())) {
            GlideUtils.loadImg(mContext, helper.getView(R.id.iv_detail_comment_icon), R.mipmap.default_user_head);
        } else {
            GlideUtils.loadImg(mContext, helper.getView(R.id.iv_detail_comment_icon), item.getUseravatar());
        }

        mGvCommentImgs = helper.getView(R.id.gv_detail_comment_imgs);

//        if (item.getImages() != null) {
//            int hspace = mContext.getResources().getDimensionPixelSize(R.dimen.dp_10);
//            int vspace = mContext.getResources().getDimensionPixelSize(R.dimen.dp_10);
//            mGvCommentImgs.setLayoutManager(new GridLayoutManager(mContext, 3));
//            mGvCommentImgs.addItemDecoration(new CommentDecoration(hspace, vspace));
//            mGvCommentImgs.setAdapter(new CommentImageAdapter(Arrays.asList(item.getImages())));
//            mGvCommentImgs.setNestedScrollingEnabled(false);
//        }

        if (!TextUtils.isEmpty(item.getImg())) {
            String[] imgarr = item.getImg().split(",");
            mGvCommentImgs.setLayoutManager(new GridLayoutManager(mContext, 3));
            if (mGvCommentImgs.getItemDecorationCount() == 0) {
                int hspace = mContext.getResources().getDimensionPixelSize(R.dimen.dp_10);
                int vspace = mContext.getResources().getDimensionPixelSize(R.dimen.dp_10);
                mGvCommentImgs.addItemDecoration(new CommentDecoration(hspace, vspace));
            } else {
            }
            final CommentImageAdapter commentImageAdapter = new CommentImageAdapter(Arrays.asList(imgarr));
            mGvCommentImgs.setAdapter(commentImageAdapter);
            mGvCommentImgs.setNestedScrollingEnabled(false);
            commentImageAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    PhotoScanActivity.startPhotoScan(mActivity, commentImageAdapter.getData(), position);
                }
            });
            mGvCommentImgs.setVisibility(View.VISIBLE);
        } else {
            mGvCommentImgs.setVisibility(View.GONE);
        }

        ExpandCommentView tvMerchantReply = helper.getView(R.id.tv_merchant_reply);
        if (!TextUtils.isEmpty(item.getHfcontent())) {
            helper.setVisible(R.id.ll_merchant_reply, true);
            int whidth = ScreenUtils.getScreenWidth(mContext) - ScreenUtils.dip2px(mContext, 80);
            tvMerchantReply.initWidth(whidth);
            tvMerchantReply.setMaxLines(2);
            tvMerchantReply.setCloseText(item.getHfcontent());
        } else {
            helper.setGone(R.id.ll_merchant_reply, false);
        }
    }
}
